<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        li {
            list-style: none;
            transition: 0.5s;
        }

        .box {
            width: 700px;
            margin: 0 auto;
            position: relative;

        }

        .box ul {
            width: 700px;
            height: 400px;
            position: relative;

        }

        .box ul li {
            width: 100%;
            height: 100%;
            position: absolute;
            left: 0;
            top: 0;
            opacity: 0;
            text-align: center;
            line-height: 400px;
            font-size: 50px;
            border: 1px solid #000;
        }

        .box ul li img {
            width: 100%;
            height: 100%;

        }

        .box ul .on {
            opacity: 1;
        }

        .jianTo {
            width: 100%;
            height: 50px;
            display: flex;
            justify-content: space-between;
            position: absolute;
            left: 0;
            top: 50%;
            margin-top: -25px;
        }

        .jianTo span {
            width: 50px;
            height: 50px;
            font-size: 30px;
            line-height: 50px;
            background: rgba(0, 0, 0, 0.7);
            text-align: center;
            color: #f2f3ef;
            cursor: pointer;
        }

        ol {
            position: absolute;
            bottom: 0;
            display: flex;
            justify-content: center;
            width: 100%;
        }

        ol li {
            width: 30px;
            height: 30px;
            background-color: #f00;
            margin: 10px;
        }

        .active {
            background-color: green;
        }
    </style>
</head>

<body>

    <div class="box">
        <ul>
            <li class="on">1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
        <ol>
            <li class="active"></li>
            <li></li>
            <li></li>
            <li></li>
        </ol>
        <div class="jianTo">
            <span class="prev">&lt;</span>
            <span class="next">&gt;</span>
        </div>
    </div>


    <script>

        function Swiper(el) {

            // 获取相关元素
            this.oBox = document.querySelector(el);
            this.oImgs = this.oBox.querySelectorAll('ul li');
            this.oDots = this.oBox.querySelectorAll('ol li');
            this.oPrev = this.oBox.querySelector('.prev');
            this.oNext = this.oBox.querySelector('.next');

            // index 控制播放第几张
            this.index = 0;
            // timer 控制定时器
            this.timer = null;

            // 自动播放功能
            this.auoPlay = function () {
                this.timer = setInterval(() => {    // 维持这个this指向轮播图
                    this.index++;
                    if (this.index === this.oImgs.length) {
                        this.index = 0;
                    }
                    this.show();
                }, 1000)
            }

            // 显示index对应的这张图片
            this.show = function () {
                // 清除所有的
                for (let i = 0; i < this.oImgs.length; i++) {
                    this.oImgs[i].classList.remove('on');
                    this.oDots[i].classList.remove('active');
                }
                // index对应的这一张显示
                this.oImgs[this.index].classList.add('on');
                this.oDots[this.index].classList.add('active');
            }

            // 上下翻页
            this.arrowEvent = function () {
                // 上翻页
                this.oPrev.onclick = () => {
                    clearInterval(this.timer);
                    this.index--;
                    if (this.index === -1) {
                        this.index = this.oImgs.length - 1;
                    }
                    this.show();
                }
                // 下翻页
                this.oNext.onclick = () => {
                    clearInterval(this.timer);
                    this.index++;
                    if (this.index === this.oImgs.length) {
                        this.index = 0;
                    }
                    this.show();
                }
            }

            // 小点切换
            this.dotEvent = function () {
                // const _this = this;
                for (let i = 0; i < this.oDots.length; i++) {
                    this.oDots[i].onclick = () => {
                        clearInterval(this.timer);
                        this.index = i;
                        this.show()
                    }
                }
            }

            // 鼠标移入移出
            this.overEvent = function () {
                this.oBox.onmouseover = () => {
                    clearInterval(this.timer)
                }
                this.oBox.onmouseout = () => {
                    this.auoPlay()
                }
            }

            this.auoPlay()
            this.arrowEvent()
            this.dotEvent()
            this.overEvent()

        }



        const s = new Swiper('.box');




    </script>

</body>

</html>